﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db;
using yq.Dictionary;
using System.Linq.Expressions;
using yq.IBase;
using System.Data;

namespace yq.Basis
{
    public abstract class BusinessBase<T> where T : ModelBase<T>, new()
    {
       
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="info"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public string Add(T info, Db.DbHelper cn)
        {
            return Add(info, Guid.NewGuid().ToString(), cn);
        }

        /// <summary>
        /// 添加(自定义key)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="keyValue"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public  string Add(T info, dynamic keyValue, Db.DbHelper cn)
        {
            info.SetKeyValue(keyValue);
           
            string SQL = new T().GetInsertColumns();
            var comm = cn.GetSqlStringCommond(SQL);
            new T().GetDbParameters(comm, info, cn);
            cn.ExecuteNonQuery(comm);
            
            //缓存操作
            cn.CaCheRefresh.Add<T>(info.GetKeyValue(), info);

            return keyValue;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="info"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public  int Update(T info, Db.DbHelper cn)
        {
            //判断是否已加载
            if (info.IsLoad == false)
            {
                throw new Exception("未加载数据,不允许更新.");
            }

            //重写版本号
            info.Version = Guid.NewGuid().ToString();

            string sql = new T().GetUpdateColumns();
            var comm = cn.GetSqlStringCommond(sql);
            new T().GetDbParameters(comm, info, cn);

            //缓存操作
            cn.CaCheRefresh.Update<T>(info.GetKeyValue(), info);

            return cn.ExecuteNonQuery(comm); ;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public bool Delete(dynamic  id, Db.DbHelper cn)
        {
            string sql = "Delete From " + new T().GetTableName() + " Where " + new T().GetTableKey() + "=@Id";
            var comm = cn.GetSqlStringCommond(sql.ToString());
            System.Data.DbType  type= SQLInParameter.GetDbType(id.GetType());
            cn.AddInParameter(comm, "@Id", type, id);
            int i = cn.ExecuteNonQuery(comm);

            //缓存操作
            cn.CaCheRefresh.Delete<T>(id);

            return i != 0;
        }


        /// <summary>
        /// 传入sql语句分页查询 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="page"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public PageList<T> GetPageList(string sql, PageModel page, Db.DbHelper cn)
        {
            int pageCount, count = 0;
            var dt = cn.Paging(sql, page.CurrentPageIndex, page.PageSize, out pageCount, out count);
            page.PageCount = pageCount;
            page.Count = count;
            PageList<T> list = new T().GetPageList(dt);
            list.PagerModel = page;

            return list;
        }


        /// <summary>
        /// 查询单表数据
        /// </summary>
        /// <param name="func"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public IList<T> GetList(Expression<Func<T, bool>> func, Db.DbHelper cn)
        {
            var comm = cn.GetSqlStringCommond("");
            string sql = WhereFunc.Where<T>(func, new SQLInParameter(cn, comm, true));
            comm.CommandText = new T().GetSelectSql()+" where " + sql;
            var dt = cn.ExecuteDataTable(comm);

            return new T().GetIList(dt);
        }

        /// <summary>
        /// 查询单表数据
        /// </summary>
        /// <param name="func"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public IList<T> GetList(Db.DbHelper cn)
        {
            var comm = cn.GetSqlStringCommond("");
            SQLInParameter parameter = new SQLInParameter(cn, comm, true);
            string sqlstring = "";

            foreach (var func in _whereList)
            {
                if (sqlstring.Length == 0)
                {
                    sqlstring = WhereFunc.Where<T>(func, parameter); 
                }
                else
                {
                    sqlstring += " AND " + WhereFunc.Where<T>(func, parameter); 
                }
            }

            if (sqlstring.Length == 0)
            {
                sqlstring = "1=1";
            }

            comm.CommandText = new T().GetSelectSql() +" where "+ sqlstring;
            var dt = cn.ExecuteDataTable(comm);

            return new T().GetIList(dt);
        }

       
        /// <summary>
        /// 查询单表分页数据
        /// </summary>
        /// <param name="func"></param>
        /// <param name="page"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public PageList<T> GetPageList(Expression<Func<T, bool>> func, PageModel page, Db.DbHelper cn)
        {
            string sql = new T().GetSelectSql()+" where ";
            sql += WhereFunc.Where<T>(func);

            return GetPageList(sql, page, cn);
        }

        /// <summary>
        /// 执行多条件查询的单表分页数据(配合多条件集合WherePlus使用)
        /// </summary>
        /// <param name="func"></param>
        /// <param name="page"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public PageList<T> GetPageList(PageModel page, Db.DbHelper cn)
        {
            string sql = new T().GetSelectSql();
            string sqlstring = "";
            foreach (var func in _whereList)
            {
                if (sqlstring.Length == 0)
                {
                    sqlstring = WhereFunc.Where<T>(func);
                }
                else
                {
                    sqlstring += " AND " + WhereFunc.Where<T>(func);
                }
            }
            if (sqlstring.Length == 0)
            {
                sqlstring = "1=1";
            }

            sql=sql + " where " + sqlstring;

            return GetPageList(sql, page, cn);
        }

        /// <summary>
        /// 多条件集合
        /// </summary>
        private IList<Expression<Func<T, bool>>> _whereList = new List<Expression<Func<T, bool>>>();

        /// <summary>
        /// 用于多条件组合
        /// </summary>
        /// <param name="func"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public void WherePlus(Expression<Func<T, bool>> func)
        {
            _whereList.Add(func);
        }



        /// <summary>
        /// 查询单表单条数据
        /// </summary>
        /// <param name="func"></param>
        /// <param name="cn"></param>
        /// <returns></returns>
        public T GetModel(Expression<Func<T, bool>> func, Db.DbHelper cn)
        {
            var comm = cn.GetSqlStringCommond("");
            string sql = WhereFunc.Where<T>(func, new SQLInParameter(cn, comm, true));
            comm.CommandText = new T().GetSelectSql() + sql;
            var dt = cn.ExecuteDataTable(comm);
            var list = new T().GetIList(dt);

            return list.Count == 0 ? new T() : list[0];
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <param name="cn"></param>
        /// <returns></returns>
        public IList<T> GetAllList(Db.DbHelper cn)
        {
            var comm = cn.GetSqlStringCommond(new T().GetSelectSql());
            var dt = cn.ExecuteDataTable(comm);
            return new T().GetIList(dt);
        }

    }
}
